package com.spdd.mt.biz.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.spdd.mt.biz.config.dynamic.DynamicTableData;
import com.spdd.mt.common.util.DateUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.Objects;


@Configuration
// @MapperScan("com.spdd.mysql.mapper*")
public class MyBatisPlusConfig {

    // @Autowired
    // private MyDynamicTableNameParser myDynamicTableNameParser;
    //
    // @Bean
    // public MyDynamicTableNameParser myDynamicTableNameParser() {
    //     return new MyDynamicTableNameParser();
    // }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType

        // 动态表名
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            if ("mt_recharge_order".equals(tableName)) {
                Map<String, Object> paramMap = DynamicTableData.getRequestData();
                String month;
                if (Objects.isNull(paramMap) || Objects.isNull(paramMap.get("month"))) {
                    month = DateUtils.getCurrentMonth();
                } else {
                    month = paramMap.get("month").toString();
                }
                // 获取表名后清除动态参数
                DynamicTableData.removeRequestData();

                // 设置最小月份为 202504
                if (Integer.parseInt(month) < 202504) {
                    month = "202504";
                }

                // 加上分表逻辑后缀
                return tableName + "_" + month;
            }

            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

        return interceptor;
    }

    // @Bean
    // public MybatisPlusInterceptor mybatisPlusInterceptor() {
    //
    //     return interceptor;
    // }


    // @Bean
    // public PaginationInterceptor paginationInterceptor() {
    //     PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    //
    //     // 创建SQL解析器集合
    //     List<ISqlParser> sqlParserList = new ArrayList<>();
    //
    //     // 动态表名SQL解析器
    //     DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
    //     Map<String, ITableNameHandler> tableNameHandlerMap = new HashMap<>(2);
    //     tableNameHandlerMap.put("dy_recharge_order", myDynamicTableNameParser);
    //
    //     dynamicTableNameParser.setTableNameHandlerMap(tableNameHandlerMap);
    //     sqlParserList.add(dynamicTableNameParser);
    //     paginationInterceptor.setSqlParserList(sqlParserList);
    //
    //     return paginationInterceptor;
    // }
}